//
//  MVDownloadManager.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 周莹. All rights reserved.
//

#import "MVDownloadManager.h"
#import "MVModel.h"
#import "MVDownloadOperation.h"
#import "DownMVViewController.h"
#define CachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
#define k_newVideoDidStartDown  @"newVideoDidStartDown"


@interface  MVDownloadOperation ()

@end

static MVDownloadManager *manager = nil;

@implementation MVDownloadManager

+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager = [[MVDownloadManager alloc] init];
            manager.httpOperationDict = [NSMutableDictionary dictionary];
            manager.downMVArray = [NSMutableArray array];
        }
    });
    return manager;

}

#pragma mark - 开始下载
-(void)startdownloadMVWithMVModel:(MVModel *)startModel{
    NSLog(@".....%@", CachesPath);
    if (!self.downLoadQueue) {
        self.downLoadQueue = [[NSOperationQueue alloc] init];
        
        self.downLoadQueue.maxConcurrentOperationCount = 3;
    }
    MVDownloadOperation *ope = [[MVDownloadOperation alloc] initWithDownloadMVModel:startModel];
#warning .......创建下载的控制器
    
    ope.updateBlock = ^(MVModel *model){
        if (self.delegate && [self.delegate respondsToSelector:@selector(MVDidUpDatedProgressWithMVModel:)]) {
            [self.delegate MVDidUpDatedProgressWithMVModel:model];
        }
    };
    
    [self.httpOperationDict setObject:ope forKey:startModel.url];
    [self.downLoadQueue addOperation:ope];
    [self.downMVArray addObject:startModel];
    [[NSNotificationCenter defaultCenter] postNotificationName:k_newVideoDidStartDown object:nil];
}
#pragma mark - 暂停下载
-(void)pausedownloadMVWithMVModel:(MVModel *)pauseModel{
    MVDownloadOperation *ope = [self.httpOperationDict objectForKey:pauseModel.url];
    if (!ope.isFinished) {
        [ope downLoadPause];
    }
}
#pragma mark - 断点继续下载
-(void)resumedownloadMVWithModel:(MVModel *)resumeModel{
    MVDownloadOperation *ope = [self.httpOperationDict objectForKey:resumeModel.url];
    if (!ope.isFinished) {
        [ope downLoadResume];
    }
}




@end
